<template>
	<view>

		<view class="" style="width: 200rpx;margin: 100rpx auto 0  auto;">
			<u-image :src="config.webLogo" width="200" height="200" mode="widthFix" border-radius="200"></u-image>

		</view>
		<view
			style="width: 500rpx;text-align: center;color: #333;margin: 40rpx auto;font-size: 34rpx;font-weight: bold;">
			{{config.siteName}}请求获取你的授权
		</view>

		<u-button shape="circle"
			:custom-style="{background:'#4f8bff',color:'#fff',marginTop:'40rpx',position: 'fixed',left:'125rpx',bottom:'100rpx',width:'500rpx'}"
			@click="appLoginWx" v-if="isregister">
			一键登录</u-button>

		<u-button shape="circle" open-type="getPhoneNumber"
			:custom-style="{background:'#4f8bff',color:'#fff',marginTop:'40rpx',position: 'fixed',left:'125rpx',bottom:'100rpx',width:'500rpx'}"
			@getphonenumber="getphonenumber" v-else>
			手机号注册</u-button>


		<view style="position: fixed;width: 200rpx;text-align: center;bottom: 40rpx;left:275rpx;color: #999999;"
			@click="close">暂时跳过
		</view>

		<u-toast ref="uToast" />


	</view>
</template>

<script>
	export default {
		data() {
			return {

				config: {},
				isregister: true,
				iv1: "",
				encryptedData1: "",
			}
		},
		onLoad(options) {
		
			if (options.isregister=='true') {
				this.isregister = false
			}
			this.getConfig()
		},
		methods: {
			getConfig() {
				this.$u.api.getConfig().then(res => {
					this.config = res;
				})
			},
			close() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			appLoginWx() {

				uni.showLoading({
					mask: true
				})

				uni.getUserProfile({
					desc: "Wexin",
					success: rese => {
						uni.login({
							success: login => {
								this.$u.post('/index/login', {
									"code": login.code,
									"encryptedData": rese.encryptedData,
									"iv": rese.iv,
									"recommend": uni.getStorageSync("uid"),
									"avatarUrl":rese.userInfo.avatarUrl,
									 'nickName':rese.userInfo.nickName,
								}).then(res => {
									this.afterLogin(res, true)
									// if (!res.mobile) {
									// 	this.isregister = false;
									// 	this.$store.commit("SET_USERINFO", res)
									// } else {
									// 	this.afterLogin(res, true)
									// }
								}).catch(err => {

								})
							},


						})
					}
				})


			},
			getphonenumber(e) {
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					uni.login({
						success: (res) => {
							var {
								encryptedData,
								iv
							} = e.detail
							this.$u.post('/user/bindMobile', {
								"encryptedData": encryptedData,
								"iv": iv,
								"recommend": uni.getStorageSync("uid")
							}).then(res => {
								var mobile = res;
								this.afterLogin(res, false, mobile)
							}).catch(err => {})
						},
					})
				} else {
					uni.showModal({
						content: '检测到您还没有打开授权手机号，是否去设置打开？',
						confirmText: "确认",
						cancelText: "取消",
						success: function(res) {
							if (res.confirm) {
								uni.openSetting({
									success: (res) => {}
								})
							} else {
								console.log('用户点击取消')
							}
						}
					})
				}
			},
			afterLogin(res, binded, mobile) {
				if (binded) {
					this.$store.commit("SET_USERINFO", res)
				} else {
					let _userInfo = this.$store.state.userInfo;
					_userInfo.mobile = mobile;
					this.$store.commit("SET_USERINFO", _userInfo)
				}
				this.$refs.uToast.show({
					title: binded ? '登录成功' : "绑定成功",
					type: 'success',
					callback: () => {
						let tabs = ["/pages/index/index", "/pages/news/index", "/pages/scan/index",
							"/pages/my/index"
						];
						if (this.$store.state.currentPagePath) {
							if (tabs.filter(item => item == this.$store.state.currentPagePath)) {
								uni.switchTab({
									url: this.$store.state.currentPagePath
								})
							}
							uni.redirectTo({
								url: this.$store.state.currentPagePath + this.$u.queryParams(this
									.$store.state
									.currentPageOption)
							})
						} else {
							if (getCurrentPages().length == 1) {
								uni.switchTab({
									url: "/pages/index/index"
								})
							} else {
								uni.navigateBack({

								})
							}
						}
					}

				})


			}



		}
	}
</script>

<style>
	.Topbg {
		height: 300rpx;
		background: #628DFF;

		border-radius: 0rpx 0rpx 50% 50%;
		display: inline-block;
		width: 750rpx;
	}

	.line {
		width: 100rpx;
		height: 14rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: auto;
	}
</style>
